package com.gemme.gemme.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.gemme.gemme.R;
import com.gemme.gemme.manager.DataManager;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

public class XydView extends RelativeLayout implements View.OnClickListener {
    private Context context;
    private OnChangeLIstener onwishvaluecharge;

    public XydView(Context context) {
        this(context,null);
    }

    public XydView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public XydView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
//        initData();
    }
    List<XyzBean.DataBean> dataLsit;
    public void initData() {
        String url = DataManager.BaseUrl_yanglinyu + "mall/shopwish/wishvalueview" ;

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(getContext()));
        DataManager.toServer3(context, url, DataManager.POST_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    XyzBean xyzBean = JSON.parseObject(response, XyzBean.class);

                    dataLsit = xyzBean.getData();
                    System.out.println("");
                    setData();
                }catch (Exception e){

                }
            }
        });
    }

    public void setData() {
        xyz1View.setVisibility(GONE);
        xyz2View.setVisibility(GONE);
        xyz3View.setVisibility(GONE);
        xyz4View.setVisibility(GONE);
        xyz5View.setVisibility(GONE);
        try {
            for (int i = 0; i < dataLsit.size(); i++) {
                XyzBean.DataBean dataBean = dataLsit.get(i);
                XyzView xyzview = null;
                if(i == 0){
                    xyzview = xyz1View;
                }else if(i == 1){
                    xyzview = xyz2View;
                }else if(i == 2){
                    xyzview = xyz3View;
                }else if(i == 3){
                    xyzview = xyz4View;
                }else if(i == 4){
                    xyzview = xyz5View;
                }

                if(xyzview != null){
                    xyzview.setVisibility(VISIBLE);
                    xyzview.setData(dataBean);
                }

                xyzview.setOnClickListener(this);
            }
        }catch (Exception e){

        }
    }


    XyzView xyz1View;
    XyzView xyz2View;
    XyzView xyz3View;
    XyzView xyz4View;
    XyzView xyz5View;
    ImageView picture;
    TextView name;
    TextView price;
    RoundCheckBox roundCheckBox;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.xyd_view,this);
        xyz1View = findViewById(R.id.xyz1View);
        xyz2View = findViewById(R.id.xyz2View);
        xyz3View = findViewById(R.id.xyz3View);
        xyz4View = findViewById(R.id.xyz4View);
        xyz5View = findViewById(R.id.xyz5View);

        picture = findViewById(R.id.picture);
        name = findViewById(R.id.name);
        price = findViewById(R.id.price);

        roundCheckBox = findViewById(R.id.rb_bianjia);


    }

    public void  setCbVisible(int isvisible){
        roundCheckBox.setVisibility(isvisible);
    }

    public void setOnChangeLIstener(OnChangeLIstener onChangeLIstener){
        roundCheckBox.setOnChangeLIstener(onChangeLIstener);
    }


    XyzView selectView ;
    @Override
    public void onClick(View view) {
        try{
            XyzView xyzView = (XyzView) view;
            XyzBean.DataBean data = xyzView.getData();
            wishvaluecharge(data);
            selectView = xyzView;
        }catch (Exception e){

        }
    }

    public void setImage(String url){
        Glide.with(context).load(url).into(picture);
    }

    private void wishvaluecharge(XyzBean.DataBean data) {
        String keyId = data.getKeyId();

        String url = DataManager.BaseUrl + "mall/shopwish/wishvaluecharge" +
                "?keyId=" +keyId;

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(getContext()));
        DataManager.toServer3(context, url, DataManager.POST_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    XyzBean xyzBean = JSON.parseObject(response, XyzBean.class);

                    dataLsit = xyzBean.getData();
                    System.out.println("");
                    setData();
                    System.out.println("");
                    if(onwishvaluecharge != null)
                        onwishvaluecharge.onChangeLIstener();
                }catch (Exception e){

                }
            }
        });
    }

    public void start() {
        initData();
    }

    public void stop() {
        setData();
    }

    public XydView setName(String namestr){
        name.setText(namestr + "");
        return this;
    }

    public XydView setPrice(String pricestr){
        price.setText("￥"+pricestr + "");
        return this;
    }

    public boolean getState() {
        return roundCheckBox.isChecked();
    }

    public void setState(boolean isCheck){
        roundCheckBox.setChecked(isCheck);
    }

    public void setOnwishvaluecharge(OnChangeLIstener onwishvaluecharge){

        this.onwishvaluecharge = onwishvaluecharge;
    }
}
